package com.example.smartagrisys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.smartagrisys.common.Result;
import com.example.smartagrisys.dto.LandCropRecordDTO;
import com.example.smartagrisys.dto.LandCropRecordQueryDTO;
import com.example.smartagrisys.service.LandCropRecordService;
import com.example.smartagrisys.util.SecurityUtil;
import com.example.smartagrisys.vo.LandCropRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;

/**
 * 土地作物记录控制器
 */
@Slf4j
@RestController
@RequestMapping("/crop-record")
public class LandCropRecordController {

    @Autowired
    private LandCropRecordService landCropRecordService;

    /**
     * 添加作物记录
     * @param recordDTO 记录信息
     * @return 添加结果
     */
    @PostMapping
    public Result<Long> add(@Valid @RequestBody LandCropRecordDTO recordDTO) {
        Long farmerId = SecurityUtil.getCurrentUserId();
        if (farmerId == null) {
            return Result.error("用户未登录");
        }
        
        Long recordId = landCropRecordService.add(recordDTO, farmerId);
        return Result.success(recordId, "添加成功");
    }

    /**
     * 更新作物记录
     * @param recordDTO 记录信息
     * @return 更新结果
     */
    @PutMapping
    public Result<Boolean> update(@Valid @RequestBody LandCropRecordDTO recordDTO) {
        Long farmerId = SecurityUtil.getCurrentUserId();
        if (farmerId == null) {
            return Result.error("用户未登录");
        }
        
        boolean success = landCropRecordService.update(recordDTO, farmerId);
        if (success) {
            return Result.success(true, "更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    /**
     * 删除作物记录
     * @param id 记录ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        Long farmerId = SecurityUtil.getCurrentUserId();
        if (farmerId == null) {
            return Result.error("用户未登录");
        }
        
        boolean success = landCropRecordService.delete(id, farmerId);
        if (success) {
            return Result.success(true, "删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 获取作物记录详情
     * @param id 记录ID
     * @return 记录详情
     */
    @GetMapping("/{id}")
    public Result<LandCropRecordVO> getDetail(@PathVariable Long id) {
        LandCropRecordVO recordVO = landCropRecordService.getDetail(id);
        return Result.success(recordVO);
    }

    /**
     * 分页查询作物记录
     * @param queryDTO 查询参数
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<Page<LandCropRecordVO>> page(LandCropRecordQueryDTO queryDTO) {
        Page<LandCropRecordVO> page = landCropRecordService.page(queryDTO);
        return Result.success(page);
    }

    /**
     * 更新作物记录状态
     * @param id 记录ID
     * @param status 状态
     * @return 更新结果
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        Long farmerId = SecurityUtil.getCurrentUserId();
        if (farmerId == null) {
            return Result.error("用户未登录");
        }
        
        boolean success = landCropRecordService.updateStatus(id, status, farmerId);
        if (success) {
            return Result.success(true, "更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    /**
     * 记录收获信息
     * @param id 记录ID
     * @param actualYield 实际产量
     * @return 记录结果
     */
    @PutMapping("/{id}/harvest")
    public Result<Boolean> recordHarvest(
            @PathVariable Long id,
            @RequestParam BigDecimal actualYield) {
        Long farmerId = SecurityUtil.getCurrentUserId();
        if (farmerId == null) {
            return Result.error("用户未登录");
        }
        
        boolean success = landCropRecordService.recordHarvest(id, actualYield, farmerId);
        if (success) {
            return Result.success(true, "记录成功");
        } else {
            return Result.error("记录失败");
        }
    }

    /**
     * 获取土地的作物记录
     * @param landId 土地ID
     * @param pageNum 页码
     * @param pageSize 每页记录数
     * @return 分页结果
     */
    @GetMapping("/land/{landId}")
    public Result<Page<LandCropRecordVO>> getLandCropRecords(
            @PathVariable Long landId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<LandCropRecordVO> page = landCropRecordService.getLandCropRecords(landId, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 获取当前农户的作物记录
     * @param pageNum 页码
     * @param pageSize 每页记录数
     * @return 分页结果
     */
    @GetMapping("/my")
    public Result<Page<LandCropRecordVO>> getMyCropRecords(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Long farmerId = SecurityUtil.getCurrentUserId();
        if (farmerId == null) {
            return Result.error("用户未登录");
        }
        
        Page<LandCropRecordVO> page = landCropRecordService.getFarmerCropRecords(farmerId, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 获取农户的作物记录
     * @param farmerId 农户ID
     * @param pageNum 页码
     * @param pageSize 每页记录数
     * @return 分页结果
     */
    @GetMapping("/farmer/{farmerId}")
    public Result<Page<LandCropRecordVO>> getFarmerCropRecords(
            @PathVariable Long farmerId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<LandCropRecordVO> page = landCropRecordService.getFarmerCropRecords(farmerId, pageNum, pageSize);
        return Result.success(page);
    }
} 